<?php
namespace app\admin\controller;
use app\admin\controller\Base;
use think\Db;
class Links extends Base
{
    public function lst()
    {
        $lst = Db::name('links')->select();
        $this->assign('lst',$lst);
        return view();
    }
    public function add_link()
    {
        if(request()->isAjax()){
            $param = input('post.');
            $param['create_time'] = time();
            $param['update_time'] = time();
            $result = Db::name('links')->insert($param);
            if($result===false){
                exit(json_encode(array('status'=>'n','info'=>'数据添加失败!')));
            }else{
                exit(json_encode(array('status'=>'y','info'=>'数据添加成功!')));
            }
        }
       return view();
    }
    public function edit_link()
    {
        if(request()->IsAjax()) {
            $id = input('post.id');
            $shujv = input('post.');
            $shujv['update_time'] = time();
            $result = Db::name('links')->where(array('id' => $id))->update($shujv);
            //$result = Db::name('links')->where(array('id' => $id))->getLastSql();
            //dump($result);exit;
            if ($result === false) {
                exit(json_encode(array('status' => 'n', 'info' => '数据修改失败!')));
            } else {
                exit(json_encode(array('status' => 'y', 'info' => '数据修改成功!')));
            }
        }else{
            $link_id = input('id');
            $link = Db::name('links')->where(array('id'=>$link_id))->find();
        }
        $this->assign('link',$link);
        return view();
    }
    public function del_link()
    {
        $id = input('post.id');
        $result = Db::name('links')->where(array('id'=>$id))->delete($id);
    }
}